﻿package  com.devilcat.ws{
	import com.devilcat.ws.Basicws;
	import com.worlize.websocket.*;
	import com.devilcat.dt.Pokemondt;
	import com.devilcat.dt.Gamedt;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	public class PVPLoadPokemonws extends Basicws{
		var uid:int;
		var itv:uint;
		var fc:Function;
		public function PVPLoadPokemonws(_uid:int,_fc:Function) {
			fc=_fc;
			super("iw-protocol");
			uid=_uid;
		}
		override function handleWebSocketOpen(event:WebSocketEvent):void
		{
			itv = setInterval(Update,200);
		}
		public function Update(){
			var pack:Object = new Object();
			pack.uid=uid;
			pack.action="pvploadpokemon";
			var myJson:String = JSONString(pack);
			websocket.sendUTF(myJson);			
		}
		override function handleWebSocketMessage(event:WebSocketEvent):void
		{
			var obj:Object = JSONObject(event.message.utf8Data)
			fc(obj[0]);
/*			//trace("bbbbbbbbbbbbb");
			if (event.message.type === WebSocketMessage.TYPE_UTF8)
			{	
			//trace("ccccccccccc");
				var obj= new Object;
			//trace("dddddddd");
			//trace("event.message.utf8Data" + event.message.utf8Data);
				obj = JSONObject(event.message.utf8Data);
			//trace("eeeeeeeeeeeeeeeee");
				//trace("obj[0].Name"+ obj[0].Name);
				//trace("obj.Name"+ obj.Name);
				if(String(obj[0].upid)==String(pokemon.upid)){
					trace("String(obj[0].upid)==String(pokemon.upid) "+String(obj[0].upid) +" " +String(pokemon.upid));
					pokemon.CurrentHP=obj[0].CurrentHP;
					trace(pokemon.Name +" "+pokemon.CurrentHP);
				}else{
					if(pokemon.uid!=Gamedt.Player.uid){
						Gamedt.BattleUI.render(obj[0].upid);
					}
				}*/
				//tes =	JSONObject(event.message.utf8Data);
				////trace("Got message: " + event.message.utf8Data);
			//}
		}

	}
	
}
